<?php
namespace app\admin\controller;
use app\admin\model\Subject as modelSubject;
use app\admin\model\Question as modelQuestion;
use app\admin\model\Exam as modelExam;
use app\admin\model\Week as weekModel;
use think\Db;
use think\facade\Request;
class Exam extends Common
{
    function index(){
        $subjectList = modelSubject::subjectList();
        $this->assign('subjectList',$subjectList);

        $inputData = input('get.');
        $where[] = ['e.is_del','=','0'];
        if($inputData['subjectid'] > 0){
            $where[] = ['e.subjectid','=',$inputData['subjectid']];
        }
        if(isset($inputData['keyword'])){
            $where[] = ['e.exam_name','like','%'.$inputData['keyword'].'%'];
        }
        $examData = modelExam::examData(input('p'),15,$where);

        $this->assign('examList',$examData['list']);
        $this->assign('examPage',$examData['page']);
        $this->assign('examCount',$examData['count']);
        $this->assign('inputData',$inputData);

        return $this->fetch('index');
    }

    // function ajaxExamList(){
    //     $inputData = input('get.');
    //     $where[] = ['e.is_del','=','0'];
    //     if($inputData['subjectid'] > 0){
    //         $where[] = ['e.subjectid','=',$inputData['subjectid']];
    //     }
    //     if(isset($inputData['keyword'])){
    //         $where[] = ['e.exam_name','like','%'.$inputData['keyword'].'%'];
    //     }
    //     $examData = modelExam::examData(input('p'),10,$where);

    //     $this->assign('examList',$examData['list']);
    //     $this->assign('examPage',$examData['page']);
    //     $this->assign('examCount',$examData['count']);
    //     return $this->fetch('ajaxExamList');
    // }
    function examLook(){
        $examModel = new modelExam();
        $examid = input('route.examid');
        $exam = $examModel->getExam($examid);
        $examSet = $examModel->putSetting($exam['exam_setting']);

//        dump($examSet[134]['question_select']['A']);exit;
//        dump($exam);
//        dump($examSet);exit;
        $this->assign('exam',$exam);
        $this->assign('questionList',$examSet);

        return $this->fetch('examLook');
    }
    //添加试卷
    function examAdd(){
        if(request()->isAjax()){
            //验证器
            (new \app\admin\validate\Exam()) -> goCheck();
//            dump($_SESSION);exit;
            $data = modelExam::disposeData(input('post.'),'add');
//            dump($data);exit;
            $res = modelExam::create($data);
            if($res){
                exitjson(1,'保存成功');
            }else{
                exitjson(0,'保存失败');
            }
        }else{
            $subjectList = modelSubject::subjectList();

//            dump($subjectList);exit;
            $this->assign('subjectList',$subjectList);
            return $this->fetch('examAdd');
        }
    }
    //编辑试卷
    function examEdit(){
        if(request()->isAjax()){
            //验证器
            (new \app\admin\validate\Exam()) -> goCheck();
            $data = modelExam::disposeData(input('post.'),'edit');
            $examid = input('post.examid');
//            dump(input('post.'));exit;
            $res = Db::name('exam')->where('examid',$examid)->update($data);
            if($res !== false){
                exitjson(1,'保存成功');
            }else{
                exitjson(0,'保存失败');
            }
        }else{
            $exam = modelExam::getExam(input('examid'));
            $subjectList = modelSubject::subjectList();

//            dump($exam);exit;
            $this->assign('exam',$exam);
            $this->assign('subjectList',$subjectList);
            return $this->fetch('examEdit');
        }
    }
    //删除试卷
    function examHide(){
        $examid = input('post.id');
        if($examid > 0){
            $data['is_del'] = 1;
            $res = Db::name('exam')->where("examid",$examid)->update($data);
            if($res !== false){
                exitjson(1,'已删除');
            }else{
                exitjson(0,'失败');
            }
        }else{
            exitjson(0,'参数错误');
        }
    }

    function ajaxQuestionSelect(){
        $weekModel = new weekModel();
        $postData = input('post.');
        $weekList = $weekModel->weekAllList();

        $this->assign('weekList',$weekList);
        $this->assign('qtypeid',$postData['qtypeid']);
        $this->assign('subjectid',$postData['subjectid']);
        return $this->fetch('ajaxQuestionSelect');
    }

    function ajaxQuestionList(){
        $where[] = ['q.is_del','=',0];
        if(input('qtypeid')>0){
            $where[] = ['q.qtypeid','=',input('qtypeid')];
        }
        if(input('subjectid')>0){
            $where[] = ['q.subjectid','=',input('subjectid')];
        }
        if(input('weekid')>0){
            $where[] = ['q.weekid','=',input('weekid')];
        }
//        dump($where);exit;
        $questionData = modelQuestion::questionData($_GET['p'],5,$where);

//        dump($questionData);exit;
        $this->assign('questionList',$questionData['list']);
        $this->assign('questionPage',$questionData['page']);
        return $this->fetch('ajaxQuestionList');
    }


}